/*
 * mserver.c
 *
 *  Created on: 2011-6-20
 *      Author: vyouzhi
 */
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>

#include "socketbase.h"
#include "link.h"
#include "BuildXml.h"
#include "conf_lib.h"


int main(){
	int sfd, cfd;
	int epollWaitNum, i=0, m;
	int recv_num;
	cliInfo *cli_h, *cli_t,  *cli_c;
	char *res, *tmp, *senddata, *plen;

	conf_init();

	sfd = Server_init();
	/*bzero(&cli_data,sizeof(cli_data));*/
	if (Epoll_init(sfd) == -1){
		exit(-1);
	}
	cli_h = link_init();	
	while(1){
		if ( 0 > (epollWaitNum = Epoll_wait())) {
			printf(" epoll_wait error\n");
			exit(-1);
		}
		for (i=0; i<epollWaitNum; i++){
			
			if(-1 == Epoll_Error(i)){
				printf("error for epoll_error mserver.c line 38 \n");
				continue;
			}
			
			if (0 == Epoll_EvteqSfd(i, sfd)){
				/*printf("evt is init for sfd mserver.c line 42\n");*/
				cfd = Socket_accept(sfd);
				Epoll_accept_init(cfd);	
	
				cli_t = link_init();
				bzero(cli_t, sizeof(cli_t));
				cli_t->clientSockfd = cfd;
				
				link_insert(cli_h, cli_t);
				printf("one client income cfd is: %d\n", cfd);
				continue;
			}
			else if (0 == Epoll_EvteqIn(i)){
				/*printf("recv data mserver.c line 48\n ");*/
				if (-1 == (cfd = Epoll_getEvtfd(i))) continue;
				cli_c = link_find(cli_h, cfd);	
				
				recv_num = Socket_recv(i, cfd, &cli_c->recvbuff);
				printf("one client read data  is : %d\n", cfd);
				m = 0;
				cli_c->recvStatus = 1;
				if(cli_c->totallen == 0){
					tmp = (char *)calloc((recv_num + 10), sizeof(char));
					bzero(tmp, (recv_num + 10));
					memcpy(tmp, cli_c->recvbuff, recv_num);
					plen = strstr(tmp, "Content-Length:");
					if(plen != NULL)
						plen = strtok(plen, "\r\n");
					if (plen != NULL)
						plen = strstr(plen, " ");
					if(plen == NULL){
						Epoll_close(i, cfd);
						link_del(cli_h, cfd);
						continue;
					}
					cli_c->totallen = atoi(plen);
					free(tmp);
				}
				
				if(cli_c->totallen ==0 || cli_c->totallen > MAXDATA){
					Epoll_close(i, cfd);		
					continue;
				}
				if(cli_c->recvCount == 0){
					tmp = (char *)calloc((recv_num + 10), sizeof(char));
					bzero(tmp, (recv_num + 10));
					memcpy(tmp, cli_c->recvbuff, recv_num);
					res = strstr(cli_c->recvbuff, "<?xml");
					if(res != 0){
						strcpy(cli_c->data, res);
						cli_c->recvCount += strlen(tmp);
					}
					free(tmp);
				}else{
					strcpy(cli_c->data+cli_c->recvCount, cli_c->recvbuff);
					cli_c->recvCount += recv_num ;

				}
	

				if (-1 == Epoll_recv_init(cfd, cli_c->totallen, cli_c->recvCount)){
					Epoll_close(i, cfd);					
					link_del(cli_h, cfd);
				}
			}
			else if (0 == Epoll_EvteqOut(i)){
				/*printf("send data mserver.c line 54 \n");*/
				if (-1 == (cfd = Epoll_getEvtfd(i))){
					link_del(cli_h, cfd);
				}
				cli_c = link_find(cli_h, cfd);
				if(cli_c->data != NULL){
					/*printf("--oo %s\n", cli_c->data);*/
					senddata = NULL;
					PaserXml(cli_c, &senddata);
					if(senddata != NULL)
						Socket_send(cfd, senddata, strlen(senddata));
				}
				free(senddata);
				Epoll_close(i, cfd);
				link_del(cli_h, cfd);
				printf("one client bye!\n");
			}
		}
	}
	link_free(cli_h);
	return (0);
}
